<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>付款退款编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/logic/paymoneyreturn";
var ItemStateList = ItemStateData.Rows;
var grid;
var tax_price=null; 
$(function(){
	var billState = '${payr.zd003Status}';
	var column1 = [
				      { display: 'id', name:'id', width: 1 ,hide:true},
					  { display: '物料id', name:'zd002MaterId',  width: 1,hide:true },
					  { display: '<font color="gray">物料编码</font>', name:'zd003MaterCode', width: 120},
					  { display: '<font color="gray">物料名称</font>', name:'zd004MaterName', width: 120},
	                  { display: '<font color="gray">大小</font>', name: 'zd011Size', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'zd012Unit', width: 120},
	                  { display: '<font color="gray">数量</font>', name: 'zd020Quantity', width: 100},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'zd013TaxPrice', width: 100}, 
	                  { display: '<font color="gray">总货款(含税)</font>', name: 'zd016Amount', width: 100,type: 'float',render: function(item){
	                	  if(item.zd016Amount == null || item.zd016Amount == "" || parseFloat(item.zd016Amount) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd016Amount*1).toFixed(4);
	                	  }
	                  }},
	                  { display: '<font color="gray">已退采购款</font>', name: 'zd017HasAmount', width: 100,type: 'float',render:function(item){
	                	  if(item.zd017HasAmount == null || item.zd017HasAmount == "" || parseFloat(item.zd017HasAmount) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd017HasAmount*1).toFixed(4);
	                	  }
	          		  }},
	                  { display: '本次退采购款<font color="red">*</font>', name: 'zd018TheAmount', width: 100,type: 'float',editor: { type: 'float'},render: function(item){
	                	  if(item.zd018TheAmount == null || item.zd018TheAmount == "" || parseFloat(item.zd018TheAmount) == 0){
	                		  return "0.0";
	                	  }else{
	                		  return (item.zd018TheAmount*1).toFixed(4);
	                	  }
	                  }}
	                  ];
	var column2 = [
	                  { display: 'id', name:'id', width: 1 ,hide:true},
	                  { display: '物料id', name:'zd002MaterId',  width: 1,hide:true },
					  { display: '<font color="gray">物料编码</font>', name:'zd003MaterCode', width: 120},
					  { display: '<font color="gray">物料名称</font>', name:'zd004MaterName', width: 120},
	                  { display: '<font color="gray">大小</font>', name: 'zd011Size', width: 270},
	                  { display: '<font color="gray">单位</font>', name: 'zd012Unit', width: 120},
	                  { display: '<font color="gray">数量</font>', name: 'zd020Quantity', width: 100},
	                  { display: '<font color="gray">单价(含税)</font>', name: 'zd013TaxPrice', width: 100}, 
	                  { display: '<font color="gray">总货款(含税)</font>', name: 'zd016Amount', width: 100},
	                  { display: '<font color="gray">已退采购款</font>', name: 'zd017HasAmount', width: 100},
	                  { display: '本次退采购款<font color="red">*</font>', name: 'zd018TheAmount', width: 100},
	                  ];
	var column3 = null;
	column3 = column1;
	grid = $("#table1").ligerGrid({
        columns: column3,
        enabledEdit: true,
        usePager: true,pageSize:1000,pageSizeOptions:[1000],
        parms:[{ name : 'payrId' , value : '${payr.id}'}],
      	url : basic_Url+"/girdItems.do",
        width: "100%", height:235, checkbox: true,
        toolbar: { items: [
                           { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
                           ]
                           }
    });
	$("#pageloading").hide();

	$("#inputForm").validate({			   
		onsubmit: true,
		 /*执行ajaxsubmit  */ 
	    submitHandler: function(editform) {
	        try{			    		
	            url =  basic_Url+"/save.do";
	            var api = frameElement.api;
	        	var W = api.opener;
	            getReturnMessage(url,"inputForm",W.searchAP);
	            W.pid='${pid}';
	        }catch(e){alert(e);}
	    }
	});
	//按钮
	if("ADD" == "${flag}"){
		$("#commit").hide();//禁用提交按钮
		$("#btn_exportExcel").hide();//禁用导出按钮
	}
	if('PAYRN_SAVE' == billState){ //草稿单据
		$("#save").hide();//禁用保存按钮
		$("#btn_exportExcel").hide();//禁用导出按钮
	}
	if('PAYRN_SUBMIT' == billState){ //完结单据
		$("#save").hide();//禁用保存按钮
		$("#commit").hide();//禁用提交按钮
		$("input[type!='hidden']").attr("disabled","disabled");
		$("select").attr("disabled","disabled");//禁用SELECT标签
		$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
	}

	//付款方式
	$("#zd008ModeId").change(function(){
		$("#zd008ModeCode").val($("#zd008ModeId").find("option:selected").attr("cvalue"));
		$("#zd008ModeName").val($("#zd008ModeId").find("option:selected").text());
	});
});

function submitForm(param){
    //设置ajax全局同步
	var data = grid.getData();
	var dl=grid.getDeleted();
	if(data.length==0){
		$.dialog.alert("请选择付款内容");
   	 	return false;
	}
	var ba=true;
	if(data !=null){
		var i=0;
		var sumsfhk=0;
		$(data).each(function (){
			i++;
			sumsfhk += parseFloat(this.zd018TheAmount);
			if(this.zd017HasAmount == null || this.zd017HasAmount.length<=0){
				this.zd017HasAmount = 0;
			}
			if((parseFloat(this.zd018TheAmount))>parseFloat(this.zd019NoAmount)){
				$.dialog.alert("您第" + i + "行的本次付款金额大于未付款金额");
				ba = false;
				return false;
			} 
			if(this.zd018TheAmount<=0){
				$.dialog.alert("您第" + i + "行的付款金额必须大于0");
				ba = false;
				return false;
			}
		});
		if(sumsfhk<=0){
			$.dialog.alert("您的本次付款金额不合法，请重新填写");
			ba = false;
			return false;
		}
	}
	jQuery.ajaxSettings.async = false;
	if(ba){
		$("#itemdata").val(JSON.stringify(data));
		$("#itemdelete").val(JSON.stringify(dl));
	  	$("#flagMark").val(param);
	    $("#inputForm").submit();
	}
 }		

function tableSubmit(msg,param){
	$.ligerDialog.confirm(msg, 
			function (flag) { 
			if(flag){
				submitForm(param);
				}
		}); 
}

function submitPay(param){
	tableSubmit('是否确认提交付款？',param);
}

function deleteRow()
{ 
	grid.deleteSelectedRow();
}

function exportExcel() {
	var id =$("#id").val();
	if(id!=""){
		var path = basic_Url + "/exportExcel.do?id="+id;
		window.open(path, "_blank");
	}else{
		$.dialog.alert("获取数据出现未知问题，请重试");
	}
}



//退货单
function  pcsmWindow() {
	getDG().dialog(
			{
				title : '选择退货单号',
				width : 650,
				height : 350,
				content : 'url:' + sys_context + '/public/windowalert/returnSing.do',    
				lock : true,
				max : true,
				min : true,
				ok : function() {
					var rows = this.content.getSelected();
					
					if(rows==null){
						$.dialog.alert("请选择数据");
						return;
					}
					else{
						if(rows.length>1){
							$.dialog.alert('只能选择一行数据!');
							return;
						}else{
							$(rows).each(function() {
						     	$("#zd002PcsrId").val(this.id);
								$("#zd002PcsrCode").val(this.zd003Code);
								$("#zd009SupeName").val(this.zd008SpName);
					    		$("#zd009SupeId").val(this.zd008SpId);
								$("#zd009SupeCode").val(this.zd008SpCode);
								$.ajax({
									url: basic_Url+"/pcsrQuary.do",
							  	      global: false,
							  	      type: "POST",
							  	      dataType: "json",
							  	      data: ({
							  	    	pcsId : this.zd001PcsId,
							  	    	pcsrId : this.id
							  	      }),		     
							  	      success: function(msg){
							  	    	  $("#zd004SumAmount").val(msg.zd004SumAmount);
							  	    	  $("#zd005HasAmount").val(msg.zd005HasAmount);
							  	    	  $("#zd010ExRateCode").val(msg.zd010ExRateCode);
							  	    	  $("#zd010ExRateName").val(msg.zd010ExRateName);
							  	    	  $("#zd010ExRateVal").val(msg.zd010ExRateVal);
							  	    	var path = basic_Url+"/pcsrItemQuery.do?pcsrId="+$("#zd002PcsrId").val();		
							  			multipleSearch("#table1",true,"",encodeURI(path));
							  	     }
								});
								
								    
					     	});
						}
					}
					this.content.Close();
					return false;
				},
				cancelVal : '关闭',
				cancel : true
			}).zindex();
	
}
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${payr.id}"/>
<input type="hidden" id="bizVersion" name="bizVersion" value="${payr.bizVersion }"/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="itemdelete" name="itemdelete" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="flagMark" name="flagMark" value="" />
 <div class="pageContent" >
    <div class="panel">
    <table class="TableBlock" align="center">
    	<c:if test="${!empty payr.zd001Code}">
    	<tr >
	        <td nowrap="nowrap">退款单号:</td>
	        <td >
	        	<input type="text" id="zd001Code" name="zd001Code" class="inp_width_98p required" readonly="readonly"  value="${payr.zd001Code}" />
	        </td>
	        <td nowrap="nowrap" width="20%">建单时间:</td>
			<td width="30%">
				<input class="inp_width_98p" type="text" id="bizCreatorDate" name="bizCreatorDate"
				value="<fmt:formatDate value="${empty payr.bizCreatorDate?nowDate:payr.bizCreatorDate}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/>
			</td>
  		</tr>
  		</c:if>
	    <tr>
    		<td nowrap="nowrap">采购退货单号:</td>
    		<td>
    			<c:choose>
    				<c:when test="${empty payr.zd002PcsrId }">
		                 <input type="hidden" id="pcsid" value="${pcsid}" />
		                 <input type="hidden" id="zd002PcsrId" name="zd002PcsrId" />
    				     <input type="text" id="zd002PcsrCode" name="zd002PcsrCode"  readonly="readonly"/>
    				     <input type="button" value="..." onclick="pcsmWindow();" />
    				</c:when>
    				<c:otherwise>
    					<input type="hidden" id="zd002PcsrId" name="zd002PcsrId" value="${payr.zd002PcsrId }"/>
    					<input type="text" id="zd002PcsrCode" name="zd002PcsrCode" value="${payr.zd002PcsrCode }" class="inp_width_98p" readonly="readonly"/>
    				</c:otherwise>
    			</c:choose>
            </td>
            <td nowrap="nowrap">供应商退款日期:</td>
            <c:choose>
            <c:when test="${empty payr.zd014ReturnDate}">
            	<td><input type="text" id="zd014ReturnDate" name="zd014ReturnDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="inp_width_98p Wdate required" value="<fmt:formatDate value="${payr.zd014ReturnDate}" pattern="yyyy-MM-dd" type="both" />" /></td>
        	</c:when>
        	<c:otherwise>
        		<td><input type="text" id="zd014ReturnDate" name="zd014ReturnDate"  class="inp_width_98p Wdate" value="<fmt:formatDate value="${payr.zd014ReturnDate}" pattern="yyyy-MM-dd" type="both" />" readonly="readonly" /></td>
        	</c:otherwise>
        	</c:choose>     
   		 </tr>
         <tr>
	     	<td nowrap="nowrap">供应商名称：</td>
	     	<td>
	     	
	     		<input type="hidden" id="zd009SupeId" name="zd009SupeId" value="${payr.zd009SupeId }"/>
	     		<input type="text" id="zd009SupeName" name="zd009SupeName" value="${payr.zd009SupeName}" class="inp_width_98p" readonly="readonly" />
	     	</td>
         	<td nowrap="nowrap">供应商编码:</td>
         	<td>
         		<input id="zd009SupeCode" name="zd009SupeCode" value="${payr.zd009SupeCode}" class="inp_width_98p" readonly="readonly"/>
         	</td> 
	     </tr>
	     <tr>
	     	<td nowrap="nowrap">付款方式:</td>
	     	<td>
	     		<c:choose>
    				<c:when test="${empty payr.zd008ModeId }">
    					<select id="zd008ModeId" name="zd008ModeId" class="inp_width_98p required" >
		            		<option></option>
		            		<c:forEach items="${paysList}" var="paym">
		            			<option value="${paym.id}" cvalue="${paym.zd001Code }">${paym.zd002Name}</option>
		            		</c:forEach>
		            	</select>
		            	<input type="hidden" id="zd008ModeCode" name="zd008ModeCode" value="${payr.zd008ModeCode }"/>
		            	<input type="hidden" id="zd008ModeName" name="zd008ModeName" value="${payr.zd008ModeName }"/>
    				</c:when>
    				<c:otherwise>
    					<input type="hidden" id="zd008ModeId" name="zd008ModeId" value="${payr.zd008ModeId }"/>
    					<input type="hidden" id="zd008ModeCode" name="zd008ModeCode" value="${payr.zd008ModeCode }"/>
    					<input type="text" id="zd008ModeName" name="zd008ModeName" value="${payr.zd008ModeName }" class="inp_width_98p" readonly="readonly"/>
    				</c:otherwise>
    			</c:choose>
		     </td>
		     <td nowrap="nowrap">币种:</td>
	     	 <td>
	     	 	<input type="hidden" id="zd010ExRateCode" name="zd010ExRateCode" value="${payr.zd010ExRateCode }"/>
	     		<input type="text" id="zd010ExRateName" name="zd010ExRateName" value="${payr.zd010ExRateName }" class="inp_width_98p"  readonly="readonly"/>
		     </td>
	     </tr>
	     <tr>  		                 
         	<td nowrap="nowrap">付款账号：</td>         	
            <td>
            <c:choose>
            <c:when test="${empty payr.zd007Account}"> 
            	<input type="text" name="zd007Account" id="zd007Account"  value="${payr.zd007Account}" class="inp_width_98p required" />
            </c:when>
            <c:otherwise>	
              <input type="text" name="zd007Account" id="skzh"  value="${payr.zd007Account}" class="inp_width_98p" readonly="readonly" />
            </c:otherwise>
            </c:choose>
            </td>
	     	<td nowrap="nowrap">汇率:</td>
	     	<td><input type="text" id="zd010ExRateVal" name="zd010ExRateVal" value="${payr.zd010ExRateVal}" class="inp_width_98p" readonly="readonly" /></td>
	     </tr>
         <tr>
	        <td nowrap="nowrap">应退总采购款:</td>
	        <td >
	        	<input type="text" id="zd004SumAmount" name="zd004SumAmount" class="inp_width_98p" readonly="readonly" value="${payr.zd004SumAmount}" />
	        </td>
	        <td nowrap="nowrap">已退采购款:</td>
	        <td >
	        	<input type="text" id="zd005HasAmount" name="zd005HasAmount" class="inp_width_98p" readonly="readonly"  value="${payr.zd005HasAmount}" />
	        	<c:if test="${payr.zd005HasAmount == null}">
					<script>
						$("#zd005HasAmount").val("0");
					</script>
				</c:if>
	        </td>
  		</tr>
		<tr>
			<td nowrap="nowrap">制单人:</td>
			<td >
			    <input type="text" id="bizCreatorName" name="bizCreatorName" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
				<c:if test="${payr.bizCreatorName != null}">
					<script>
						$("#bizCreatorName").val("${payr.bizCreatorName}");
					</script>
				</c:if>
		  </td>
		</tr>
         <tr>
         	<td nowrap="nowrap">备注:</td>
         	<td colspan="3"><input id="zd013Remark" type="text" class="inp_width_98p" name="zd013Remark" value="${payr.zd013Remark}" /></td>
         </tr>
     </table>
     <div id="table1"> </div>               
     </div>
    <div class="formBar">
	        <ul>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('PAYRN_SAVE')">保存草稿</button></div></div></li>
		       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="commit" onclick="submitPay('PAYRN_SUBMIT')">提交付款</button></div></div></li>
			   <li><div class="button"><div class="buttonContent"><button type="button" id="btn_exportExcel" class="export" onclick="exportExcel()">导出</button></div></div> </li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
